处理json和表单参数的JavaSpringMVC方法
我想要在single spring mvc方法中处理内容类型application/x-www-form-urlencoded和application/json
我在rest服务中有一个要求,即接受作为表单参数或json的输入。我可以通过编写两种方法来实现这一点。无论是formparams还是json,响应都将始终是json
@RequestMapping (method = RequestMethod.POST, produces = {"application/json"},
consumes = {"application/x-www-form-urlencoded"})
public @ResponseBody Book createBook(Book book)
throws Exception {
return book;
}
@RequestMapping (method = RequestMethod.POST, produces = {"application/json"},
consumes = {"application/json"})
public @ResponseBody Book createBookJSON(@RequestBody Book book)
throws Exception {
return book;
}
有没有可能将这两种方法结合在一起并使其发挥作用?任何帮助都将不胜感激
编辑
我已经实现了相同的功能,下面给出了我的控制器和配置,但是当我发送json请求时,我会得到空值作为响应
当我发送表单参数时,它工作正常。帮我找出问题所在
控制器方法
@RequestMapping (method = RequestMethod.POST, produces = {"application/json", "application/xml"}, consumes = {"application/x-www-form-urlencoded", "application/json"})
public @ResponseBody Book createBook(Book book)
throws Exception {
return book;
}
servlet上下文
<mvc:view-controller path="/" view-name="index"/>
<context:annotation-config />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<!-- JAXB XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<ref bean="jaxb2Marshaller" />
</constructor-arg>
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="order" value="1" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name="supportedMediaTypes" value="application/json"/>
</bean>
<bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean class = "org.springframework.http.converter.FormHttpMessageConverter">
<property name="supportedMediaTypes" value = "application/x-www-form-urlencoded" />
</bean>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>
</list>
</property>
</bean>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
<property name="classesToBeBound">
<list>
<value>com.lt.domain.Book</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
# 1 楼答案
如果您想让spring自动从JSON转换为您的对象,您必须使用
@RequestBody
。如果您不使用它,那么这些值将不会被绑定,并且所有参数都是空的,正如您在发送JSON时所描述的那样如果您使用
@RequestBody
:在幕后,转换是使用MessageConverter完成的,如spring文档中所述:http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-requestbody您正在正确配置
MappingJacksonHttpMessageConverter
,它将JSON转换为您的对象缺少的是从表单数据到对象的转换: 您注册了一个
FormHttpMessageConverter
,但根据文档,这并不是您期望它做的。它使用x-www-form-urlencode将请求转换为MultiValueMap<String, String>
,而不是转换为自定义对象我目前的理解是,您必须编写类似于
MappingJacksonHttpMessageConverter
的东西,但只能处理表单数据# 2 楼答案
consumes获取一个字符串数组,该数组包含它可以使用的任何内容,Springbean绑定应该负责其余部分。问题可能是您没有正确设置bean绑定以便自动整理和取消整理json。使用@RequestBody和@RepsonseBody并不是imho的最佳选择
确保jackson已添加到您的依赖项中
并使用ContentNegotingViewResolver
确保在客户端应用程序中将accept标头设置为所需的值。 您还应该能够删除requestmethod注释中的所有生产和消费数据